package com.spc.car.web.controller.org;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.constant.OrganizationConstants;
import com.spc.car.data.domain.org.po.*;
import com.spc.car.data.domain.org.vo.ListOrgVO;
import com.spc.car.data.domain.org.vo.OrgPersonnelInformListVO;
import com.spc.car.data.entity.org.*;
import com.spc.car.data.service.org.*;
import com.spc.car.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.Date;

@Api(tags = "机构管理")
@Validated
@Slf4j
@RestController
@RequestMapping("/srganizational/management")
public class OrganizationalManagementController extends BaseController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private OrgQualificationService orgQualificationService;

    @Autowired
    private OrgRewPunService orgRewPunService;

    @Autowired
    private OrgEmployeeService orgEmployeeService;

    @Autowired
    private OrgPersonnelInformationService orgPersonnelInformationService;

    @ApiOperation(value = "新增机构", notes = "新增机构")
    @PostMapping("/addOrg")
    public AjaxResult addOrg(@Valid @RequestBody SaveOrgPO saveOrgPo){
        if (OrganizationConstants.UNIQUE.equals(orgService.uniqueOrganizationCode(saveOrgPo.getCode(),saveOrgPo.getId()))){
            Org org = new Org();
            BeanUtils.copyProperties(saveOrgPo,org);
            org.setCreateTime(new Date());
            orgService.save(org);
            return AjaxResult.success("新增机构"+saveOrgPo.getName()+"成功");
        }
        return AjaxResult.error("新增机构"+saveOrgPo.getName()+"失败，统一信用代码已存在");
    }

    @ApiOperation(value = "修改机构", notes = "修改机构")
    @PutMapping("/updateOrg")
    public AjaxResult updateOrg(@Valid @RequestBody SaveOrgPO saveOrgPo){
        if (OrganizationConstants.UNIQUE.equals(orgService.uniqueOrganizationCode(saveOrgPo.getCode(),saveOrgPo.getId()))){
            Org org = new Org();
            BeanUtils.copyProperties(saveOrgPo,org);
            org.setModifyTime(new Date());
            return toAjax(orgService.updateOrganization(org));
        }
        return AjaxResult.error("修改机构"+saveOrgPo.getName()+"失败，统一信用代码已存在");
    }

    @ApiOperation(value = "获取机构列表", notes = "获取机构列表")
    @PostMapping("/listOrg")
    public AjaxResult listOrg(@Valid @RequestBody GetOrgListPO<ListOrgVO> po){
        return AjaxResult.success(orgService.selectOrgList(po));
    }

    @ApiOperation(value = "通过机构ID查询机构信息", notes = "通过机构ID查询机构信息")
    @GetMapping("/queryOrgById")
    public AjaxResult queryOrgById(@RequestParam(name = "orgId",required = false) String orgId){
        return AjaxResult.success(orgService.getById(orgId));
    }

    @ApiOperation(value = "查询机构信息", notes = "查询机构信息")
    @GetMapping("/queryOrg")
    public AjaxResult queryOrg(@RequestParam(name = "orgName",required = false) String orgName){
        return AjaxResult.success(orgService.queryOrgByOrgName(orgName));
    }

    @ApiOperation(value = "新增/修改机构资质", notes = "saveOrgQualifications为新增机构资质，updateOrgQualifications为修改机构资质")
    @PostMapping({"/saveOrgQualifications","/updateOrgQualifications"})
    public AjaxResult saveOrUpdateOrgQualifications(@Valid @RequestBody SaveOrgQualificationsPO saveOrgQualificationsPo){
        return toAjax(orgQualificationService.saveOrgQualification(saveOrgQualificationsPo));
    };

    @ApiOperation(value = "获取机构资质列表", notes = "获取机构资质列表")
    @PostMapping("/orgQualificationsList")
    public AjaxResult orgQualificationsList(@Valid @RequestBody GetOrgQualificationsListPO getOrgQualificationsListPo){
        return AjaxResult.success(orgQualificationService.selectOrgQualificationList(getOrgQualificationsListPo));
    }

    @ApiOperation(value = "新增/修改机构奖惩记录", notes = "saveOrgRewPun为新增机构奖惩记录，updateOrgRewPun为修改机构奖惩记录")
    @PostMapping({"/saveOrgRewPun","/updateOrgRewPun"})
    public AjaxResult saveOrUpdateOrgRewPun(@Valid @RequestBody SaveOrgRewPunPO saveOrgRewPunPo){
        OrgRewPun orgRewPun = new OrgRewPun();
        BeanUtils.copyProperties(saveOrgRewPunPo,orgRewPun);
        if (null == saveOrgRewPunPo.getId() || 0 ==saveOrgRewPunPo.getId()){
            orgRewPun.setCreateTime(new Date());
        }else {
            orgRewPun.setModifyTime(new Date());
        }
        if (orgRewPunService.saveOrUpdate(orgRewPun)){
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");
    }

    @ApiOperation(value = "获取机构奖惩记录列表", notes = "获取机构奖惩记录列表")
    @PostMapping("/orgRewPunList")
    public AjaxResult orgRewPunList(@Valid @RequestBody GetOrgRewPunListPO getOrgRewPunListPO){
        return AjaxResult.success(orgRewPunService.selectOrgRewPunList(getOrgRewPunListPO));
    }

    @ApiOperation(value = "新增/修改机构雇员信息", notes = "新增/修改机构雇员信息")
    @PostMapping("/saveOrUpdateEmployee")
    public AjaxResult saveOrUpdateEmployee(@Valid @RequestBody SaveOrgEmployeePO saveOrgEmployeePO){
        if (orgEmployeeService.uniqueEmployee(saveOrgEmployeePO)){
            return toAjax(orgEmployeeService.saveEmployeeInformation(saveOrgEmployeePO));
        }
        return AjaxResult.error("操作失败，该身份证信息的人在机构中已存在，请核对信息");
    }

    @ApiOperation(value = "获取机构雇员列表", notes = "获取机构雇员列表")
    @PostMapping("/employeeList")
    public AjaxResult employeeList(@Valid @RequestBody GetEmployeeListPO getEmployeeListPO){
        return AjaxResult.success(orgEmployeeService.getEmployeeList(getEmployeeListPO));
    }

    @ApiOperation(value = "新增/修改雇员个人基本信息", notes = "新增/修改雇员个人基本信息")
    @PostMapping("/saveOrUpdatePersonnelInform")
    public AjaxResult saveOrUpdatePersonnelInform(@Valid @RequestBody SavePersonnelInformPO savePersonnelInformPO){
        if (orgPersonnelInformationService.uniquePersonCode(savePersonnelInformPO.getPersonCode(), savePersonnelInformPO.getId())){
            return toAjax(orgPersonnelInformationService.saveOrgPersonnelInformation(savePersonnelInformPO));
        }
        return AjaxResult.error("操作失败，该身份证信息的人已存在，请核对信息");
    }

    @ApiOperation(value = "获取雇员个人基本信息", notes = "通过雇员身份证号，获取雇员个人基本信息列表")
    @GetMapping("/personnelInformByIDCard")
    public AjaxResult personnelInformByIDCard(@Pattern(regexp = "^\\d{15}|^\\d{17}([0-9]|X|x)$", message = "身份证号码格式不正确")
                                                  @RequestParam String personCode){
        OrgPersonnelInformation orgPersonnelInformation = orgPersonnelInformationService.getPersonnelInform(personCode);
        if (ObjectUtils.isNotNull(orgPersonnelInformation)){
            OrgPersonnelInformListVO orgPersonnelInformListVO = new OrgPersonnelInformListVO();
            BeanUtils.copyProperties(orgPersonnelInformation, orgPersonnelInformListVO);
            return AjaxResult.success(orgPersonnelInformListVO);
        }
        return AjaxResult.success("新雇员无个人基本信息，请新增");
    }
}
